package com.vadmin.vadminjava.common;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TooUtil {


    //是否为空
    public static Boolean isEmpty(String o){
        return StringUtils.isEmpty(o);
    }

    /**
     6      * 时间戳转换成日期格式字符串
     7      * @param seconds 精确到秒的字符串
     8      * @param formatStr
     9      * @return
     10      */
    public static String timeStamp2Date(String seconds) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

    /**
     * 获取传入时间的后一天数据
     */
    public static Date getDayAfter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获得指定时间
     */
    public static Date getTheSpecifiedTime(Date executionTime, int h, int m, int mi) {

        //获得指定时间的23点59分59秒
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(executionTime);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), h, m, mi);
        return calendar.getTime();
    }

    /**
     * 转 int
     *
     * @description:
     * @param:
     * @return:
     * @author
     * @date: 2021/6/4 11:45
     */
    public static int forcedConversion(Object param) {
        try {
            return Integer.parseInt(param.toString());
        } catch (Exception e) {
            return 0;
        }
    }

    //获取本周的开始时间
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    //获取本周的结束时间
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }


    // 获取上周的结束时间
    public static String getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return CommonUtils.simpleDateFormat.format(getDayStartTime(cal.getTime()));
    }

    // 获取上周的结束时间
    public static String getEndDayOfLastWeek() {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(CommonUtils.simpleDateFormat.parse(getBeginDayOfLastWeek()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return CommonUtils.simpleDateFormat.format(getDayEndTime(weekEndSta));
    }

    //获取本周第一天数据
    public static String getWeekStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date date = cal.getTime();
        return CommonUtils.simpleDateFormat.format(getDayEndTime(date));
    }

    /**
     * 得到几天前
     *
     * @description:
     * @param:
     * @return:
     * @author
     * @date: 2021/12/29 15:54
     */
    public static String getDayQian(int day) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -day);
        return CommonUtils.simpleDateFormat.format(cal.getTime());
    }

    //格式化短时间
    public static String getShortData(String format, String date) {
        try {
            return new SimpleDateFormat(format).format(CommonUtils.simpleDateFormat.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


}
